import { isLogin, toLogin, getUserInfo, setUserInfo } from '../../utils/auth'
import area from '../../utils/area';
import { checkPhone } from "../../utils/validate";
import ValidateForm from "../../utils/validateForm";
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'
import http from '../../utils/http'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        canSendSms: true,
        time: 10000,

        mobile: null,
        validCode: null,
        nickname: null,
        password: null,
        province: null,
        city: null,
        district: null,
        refereeCode: null,

        showPopup: false,
        areaList: [],
        chooseAreaValue: [],

        validate: null,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // 初始化地区数据
        this.setData({
            areaList: {
                province_list: area.province_list,
                city_list: area.city_list,
                county_list: area.county_list,
            }
        });

        this.initValidate();
    },

    /**
     * 提交表单
     * @param {*} e 
     */
     async formSubmit({detail}) {
        let params = detail.value;

        if (!this.validate.checkForm(params)) {
            let error = this.validate.errorList[0];
            
            switch (error.param) {
                case "mobile":
                    showNotify(this, error.msg, 'danger'); break;
                case "validCode":
                    showNotify(this, error.msg, 'danger'); break;
                case "region":
                    showNotify(this, error.msg, 'danger'); break;
                case "nickname":
                    showNotify(this, error.msg, 'danger'); break;
                case "password":
                    showNotify(this, error.msg, 'danger'); break;
            }
            return false;
        }

        let form = Object.assign({}, params)
        form.province = form.region.split(',')[0]
        form.city = form.region.split(',')[1]
        form.district = form.region.split(',')[2]
        console.log(form);
        
        let res = await http.post('/api/mall/umsmember/register', form)

        if(res.status) {
            // 将用户信息保存到缓存中
            setUserInfo(res.data)

            // 将用户信息保存到全局属性
            getApp().globalData.userInfo = res.data;

            wx.switchTab({ url: '/pages/index/index' })
        } else {
            showToast({title: res.msg})
        }

    },

    initValidate() {
        let rules = {
            mobile: {
                required: true,
                tel: true
            },
            validCode: {
                required: true,
            },
            region: {
                required: true
            },
            nickname: {
                required: true
            },
            password: {
                required: true
            }
        }
        let message = {
            mobile: {
                required: '请输入手机号',
                maxlength: '请输入有效的手机号'
            },
            validCode: {
                required: '请输入验证码'
            },
            region: {
                required: '请选择所在地区'
            },
            nickname: {
                required: '请填写会员名'
            },
            password: {
                required: '请填写登录密码'
            }
        }
        //实例化当前的验证规则和提示消息
        this.validate = new ValidateForm(rules, message);
    },

    handleShowPopup() {
        this.setData({ showPopup: true });
    },
    handleHidePopup() {
        this.setData({ showPopup: false });
    },

    /**
     * 选中所在地区
     * @param {*} e 
     */
    handleConfirmArea(e) {
        const { values } = e.detail;
        // 选中的地区
        let region = [values[0].name, values[1].name, values[2].name];
        this.setData({ showPopup: false, region });
    },

    // 监听手机号输入框
    inputMobileListen(e) {
        this.setData({ mobile: e.detail.value })
    },

    /**
     * 获取短信验证码
     */
    async sendMsg() {
        let { canSendSms, mobile } = this.data
        if(!checkPhone(mobile)) {
            showToast({title: '请输入手机号'})
            return false;
        }

        if (!canSendSms) return

        let res = await http.request('/api/mall/umsmember/sendSmsCode', {mobile: this.data.mobile})

        if(res.status) {
            // 开始倒计时
            this.setData({ canSendSms: false })
            this.selectComponent('.control-count-down').start()
        } else {

        }
    },

    /**
     * 倒计时结束触发
     */
     countDownFinished() {
        this.setData({ canSendSms: true })
    },
})